package cn.seocoo.platform.api.controller.car;

import cn.seocoo.platform.api.util.JSONResult;
import cn.seocoo.platform.car.domain.CarInfo;
import cn.seocoo.platform.car.dto.CarDetailDto;
import cn.seocoo.platform.car.service.ICarInfoService;
import cn.seocoo.platform.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: ApiCarSearchController
 * @description: 车辆搜索
 * @author: wujian
 * @Date: 2019-12-31 15:29
 **/
@RestController
@RequestMapping("/api/carInfo")
public class ApiCarInfoController {
    @Autowired
    private ICarInfoService iCarInfoService;

    /**
     * @Author WuJian
     * @Description //获取车辆详情
     * @Date 9:54 2020/1/6
     * @Param [id, appUserId]
     * @return cn.seocoo.platform.api.util.JSONResult
     **/
    @GetMapping("/getCarDetail")
    public JSONResult getCarDetail(Integer id,Integer appUserId) {
        try {
            if(appUserId == null){
                return JSONResult.errorMsg(JSONResult.NOT_FOUND_USER);
            }
            CarDetailDto carDetailDto = iCarInfoService.getCarDetail(id,appUserId);
            return JSONResult.ok(carDetailDto);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        }

    }


    /**
     * @Author WuJian
     * @Description //车辆的上下架操作
     * @Date 14:34 2020/1/3
     * @Param [id, appUserId]
     * @return cn.seocoo.platform.api.util.JSONResult
     **/
    @GetMapping("/shelfCar")
    public JSONResult shelfCar(Integer id,String shelfStatus) {
        try {
            if(id == null){
                return JSONResult.errorMsg("请选择车辆");
            }
            if(StringUtils.isBlank(shelfStatus)){
                return JSONResult.errorMsg("请选择操作类型");
            }
            CarInfo carInfo = iCarInfoService.selectCarInfoById(id);
            if(carInfo != null){
                int row = iCarInfoService.shelfCar(id,shelfStatus);
                if(row > 0){
                    if(CarInfo.SHELF.equals(shelfStatus)){
                        return JSONResult.ok("上架成功");
                    }else if(CarInfo.NO_SHELF.equals(shelfStatus)){
                        return JSONResult.ok("下架成功");
                    }

                }
            }else{
                return JSONResult.errorMsg("当前选择车辆不存在");
            }


            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        } catch (Exception e) {
            return JSONResult.errorMsg(JSONResult.ERROR_MSG);
        }

    }



}
